var blank=imgBasePath+"blank.gif";
var minus010=imgBasePath+"minus010.gif";
var minus110=imgBasePath+"minus110.gif";
var minus111=imgBasePath+"minus111.gif";
var plus111=imgBasePath+"plus111.gif";
var plus110=imgBasePath+"plus110.gif";
var line101=imgBasePath+"line101.gif";
var line111=imgBasePath+"line111.gif";
var line110=imgBasePath+"line110.gif";

$(window).addEvent('domready',function(){
    var stack=[],pnode;   //堆栈以保存父节点序列，即栈顶元素是当前节点的父节点
    var prependImg={};      //保存前置图像放法的对象
    prependImg["p1"]=[];
    var style=(Browser.Engine.trident==true)?"block":"table-row";   //FUCK IE
    for(var i=0,len=Nodes.length;i<len;i++){
        var node=Nodes[i];
        var td=$("node"+node.id);   //要插入图片的TD
        if(node.level==1){
            td.grab(new Element("img",{"src":minus010}),"top");
            stack.push(i);
            pnode=node;
            Nodes[i].lastInLayer=true;
            td.parentNode.style.display=style;
        }else{
            var diff=node.level-pnode.level;
            if(diff>1){         //当前节点所处的层级恰好是“父节点”层级加1，则说明正好是父节点
                var j=i-1;
                stack.push(j);
                pnode=Nodes[j];
            }else if(diff<1){  //当前节点和“父节点”是同层级的，则说明要出栈，重设父节点
                while(diff<1){
                    diff++;
                    stack.pop();
                }
                pnode=Nodes[stack[stack.length-1]];
            }
            if(node.lft+1==node.rgt){    //说明没有子节点了
                if(node.rgt+1==pnode.rgt){       //是当前节点所处层级中的最后一个节点
                    imgObj=new Element("img",{"src":line110});
                    Nodes[i].lastInLayer=true;
                }else{
                    imgObj=new Element("img",{"src":line111});
                }
            }else{      //包含子节点
                if(node.rgt+1==pnode.rgt){
                    imgObj=new Element("img",{"src":plus110,"class":"plus"});
                    imgObj.store("imgtype","110");
                    Nodes[i].lastInLayer=true;
                }else{
                    imgObj=new Element("img",{"src":plus111,"class":"plus"});
                    imgObj.store("imgtype","111");
                }
                //绑定事件
                imgObj.addEvent('click',function(){
                    var tr=this.getParent().getParent();
                    var level=tr.retrieve("level");
                    if(this.className=="plus"){  //说明当前是+号
                        this.className="minus";
                        this.src=(this.retrieve("imgtype")=="110")?minus110:minus111;
                        while(true){
                            tr=tr.getNext();
                            if(!tr) break;
                            var diff=tr.retrieve("level")-level;
                            if(diff==1){
                                tr.style.display=style;
                            }else if(diff==0){
                                break;
                            }
                        }
                    }else{
                        this.className="plus";
                        this.src=(this.retrieve("imgtype")=="110")?plus110:plus111;
                        while(true){    //收缩时的效果
                            tr=tr.getNext();
                            if(!tr) break;
                            var diff=tr.retrieve("level")-level;
                            if(diff>=1){
                                tr.style.display="none";
                                var img=tr.getElements("img").getLast();
                                if(img.className=="minus"){
                                    img.className="plus";
                                    img.src=(img.retrieve("imgtype")=="110")?plus110:plus111;
                                }
                            }else if(diff==0){
                                break;
                            }
                        }
                    }
                });
            }
            var tr=td.getParent();
            tr.store("level",node.level);
            tr.addEvents({
            	'mouseover':function(){
            		this.getElement('span').setStyle('display','inline');
            	},
            	'mouseout':function(){
            		this.getElement('span').setStyle('display','none');
            	}
            });
            td.grab(imgObj,"top");
            //插入前置的图像，先继承父节点
            var inherit=$A(prependImg["p"+pnode.id]);
            inherit.push( (pnode.lastInLayer)?"a":"b" );//b代表是竖线，a代表是空白图像
            prependImg["p"+node.id]=inherit;
            for(var m=-1,n=inherit.length-1;n>m;n--){
                if(inherit[n]=="a") td.grab(new Element("img",{"src":blank}),"top");
                else td.grab(new Element("img",{"src":line101}),"top");
            }
            //决定是否显示当前行
            if(node.level<=2){
                td.parentNode.style.display=style;
            }
        }
    }
});